<?php
/**
 * Created by PhpStorm.
 * User: 共赢岛
 * Date: 2023/12/2
 * Time: 16:36
 */

namespace App\Http\Resources;

use App\Admin\Services\CarService;
use App\Services\LifeCycle\OrderService;
use App\Services\LifeCycle\UserCarsService;
use Illuminate\Http\Resources\Json\JsonResource;

class WorkOrderDetailResource extends  JsonResource
{
    public function toArray($request)
    {
        $user_car = UserCarsService::one(['order_id' => $this->id]);

        if (isset($user_car) && $user_car->car_number) {
            $car_number = $user_car->car_number;
        } else {
            $car_number = '';
        }

        return [
            'order_id' => $this->id,
            'order_sn' => $this->order_sn,
            'order_status' => $this->status,
            'order_status_title' => OrderService::orderStatusTitle($this->status),
            'brand_id' => $this->brand_id,
            'brand_title' => $this->brand->title,
            'brand_pic' => path2url($this->brand->pic),
            'category_id' => $this->category_id,
            'package_title' => $this->package_title,
            'package_category_title' => $this->package->type == PACKAGE_TYPE_SALE ? $this->category->title : '售后'.$this->category->title,
            'package_cars' => CarService::carJsonSwitch($this->category->car_ids),
            'package_pic' => path2url($this->package_pic),
            'store_id' => $this->store_id,
            'store_title' => $this->store->title,
            'user_name' => $this->user_name ?? '',
            'user_phone' => $this->user_phone ?? '',
            'car_frame' => $this->car_frame ?? '',
            'transact_name' => $this->transact_name ?? '',
            'price' => $this->price,
            'original_price' => $this->package->original_price,
            'order_price' => $this->order_price,
            'created_at' => $this->created_at ? $this->created_at->format('Y-m-d H:i:s') : '',
            'payment_at' => $this->payment_at ?? '',
            'payment_sn' => $this->payment_sn ?? '',
            'payment_type' => $this->payment_type,
            'code_url' => path2url($this->code_url),
            'code_number' => $this->code_number,
            'car_number' => $car_number,
            'refund_price' => $this->refund_price,
            'refund_sn' => $this->refund_sn,
            'refund_type' => $this->refund_type,
            'refund_status' => $this->refund_status,
            'remarks' => $this->remarks,
            'remarks_pics' => json_decode($this->remarks_pics, true)
        ];
    }
}